package com.csdj.t309springboot.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;


public class HttpRestUtil {

    private static Logger log = LoggerFactory.getLogger("httputil");

    public static <T> T requestForForm(String url, HttpMethod method, MultiValueMap<String, String> data, Class<T> clazz){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/x-www-form-urlencoded");
        headers.add("Accept","application/json");
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(data,headers);
        try{
            ResponseEntity<T> resp = restTemplate.exchange(url, method,httpEntity,clazz);
            if(resp.getStatusCode() == HttpStatus.OK){
                return resp.getBody();
            }else{
                return null;
            }
        }catch (HttpClientErrorException e){
            log.error("http error uri:{}  response code:{}",url,e.getStatusCode());
            return null;
        }

    }

    public static <T> T requestForJson(String url, HttpMethod method, Object data, Class<T> clazz){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Accept","application/json");
        HttpEntity<Object> httpEntity = new HttpEntity<>(data,headers);
        ResponseEntity<T> resp = restTemplate.exchange(url, method,httpEntity,clazz);
        if(resp.getStatusCode() == HttpStatus.OK){
            return resp.getBody();
        }
        log.error("http error uri:{}  response code:{}",url,resp.getStatusCode());
        return null;
    }

    public static <T> T post(String url, MultiValueMap<String, String> data, Class<T> clazz){
        return requestForForm(url, HttpMethod.POST,data,clazz);
    }

    public static <T> T get(String url,Class<T> clazz){
        return requestForForm(url, HttpMethod.GET,null,clazz);
    }

    public static void main(String[] args) throws InterruptedException {
        TreeMap<String,String>map = new TreeMap<>();
        map.put("username","cloud");
        map.put("email","test@test.com");
        map.put("nickname","test");
        map.put("desc","desc");
        map.put("accessKey","abcdefg");

        String secretKey = "wienq31fj2ijsf21f2g3";
        StringBuilder sb = new StringBuilder();
        map.entrySet().forEach(item ->{
            sb.append(item.getKey()).append("=").append(item.getValue()).append("&");
        });
        sb.append("secretKey=").append(secretKey);
        String sign = EncryptUtil.getSha256(sb.toString());

        System.out.println(sb);
        System.out.println(sign);
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("sign",sign);
        params.add("username","cloud");
        params.add("email","test@test.com");
        params.add("nickname","test");
        params.add("desc","descc");
        params.add("accessKey","abcdefg");

        String resp = post("http://localhost:8081/api/user/callback",params,String.class);
        System.out.println(resp);
//        String res = post("https://www.pixel-infinity.com/api/pixel/draw",params, String.class);
//        System.out.println(res);


    }



}
